
import 'package:bloc_usage/bloc/BlocProvider.dart';
import 'package:bloc_usage/bloc/HomeDrawerBloc.dart';
import 'package:bloc_usage/bloc/MainBloc.dart';
import 'package:bloc_usage/bloc/repo_bloc.dart';
import 'package:bloc_usage/model/StickerModel.dart';
import 'package:bloc_usage/page/main_page.dart';
import 'package:bloc_usage/page/new_page.dart';
import 'package:bloc_usage/page/repo_page.dart';
import 'package:bloc_usage/page/system_page.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  List<String> _pages = ["首页", "项目", "动态", "体系"];

  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  var timer;
  var count = 0;

  List<StickerModel> models = [
    StickerModel("StikcerOne", ""),
    StickerModel("StickerTwo", ""),
    StickerModel("StickerThree", ""),
  ];
  HomeDrawerBloc homeDrawerBloc;
  MainBloc mainBloc;
  @override
  void initState() {
    homeDrawerBloc = HomeDrawerBloc();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: widget._pages.length,
        child: Scaffold(
          appBar: AppBar(
            leading: Icon(Icons.navigation),
            title: TabBar(
              tabs: widget._pages.map((name) {
                return Tab(text: name,);
              }).toList(),
            ),
          ),
          body: TabBarView(
            children: <Widget>[
              BlocProvider(
                bloc: MainBloc(),
                child: MainPage(),
              ),
              BlocProvider(
                bloc: RepoBloc(),
                child: RepoPage(),
              ),
              BlocProvider(
                bloc: RepoBloc(),
                child: NewPage(),
              ),
              BlocProvider(
                bloc: RepoBloc(),
                child: SystemPage(),
              ),
            ],
          ),
        ));

    // mainBloc = BlocProvider.of<MainBloc>(context);
    // initMainData();
    // Future.delayed(Duration(seconds: 2), () {
    //   mainBloc.mainSink.add(MainModel());
    // });
    // timer = Timer.periodic(Duration(seconds: 1), (timer) {
    //   count++;
    //   homeDrawerBloc.changeCount(count);
    //   if (count > 15) timer.cancel();
    // });

    // return StreamBuilder<MainModel>(
    //     stream: mainBloc.mainStream,
    //     builder: (context, AsyncSnapshot<MainModel> snapshot) {
    //       TabController tc;
    //       if (snapshot.data != null)
    //         tc = TabController(
    //             length: snapshot.data.stickerModels.length, vsync: this);

    //       return Scaffold(
    //         appBar: AppBar(
    //           leading: Icon(Icons.nature),
    //           centerTitle: true,
    //           title: TabBar(
    //             controller: snapshot.data == null
    //                 ? TabController(length: 0, vsync: this)
    //                 : tc,
    //             isScrollable: true,
    //             indicatorSize: TabBarIndicatorSize.label,
    //             labelPadding: EdgeInsets.all(12.0),
    //             labelColor: Colors.red,
    //             tabs: snapshot.data == null
    //                 ? <Tab>[Tab(text: "loading")]
    //                 : snapshot.data.stickerModels.map((model) {
    //                     return Tab(
    //                       child: Text(
    //                         model.name,
    //                         style: TextStyle(color: Colors.white, fontSize: 15),
    //                       ),
    //                     );
    //                   }).toList(),
    //           ),
    //         ),
    //         body: snapshot.data == null
    //             ? SizedBox(
    //                 width: 30,
    //                 height: 30,
    //                 child: CircularProgressIndicator(
    //                   strokeWidth: 1.5,
    //                 ),
    //               )
    //             : TabBarView(
    //                 controller: tc,
    //                 children: snapshot.data.stickerModels.map(
    //                   (model) {
    //                     return StickerPage(model);
    //                   },
    //                 ).toList()),
    //         drawer: Drawer(
    //           child: BlocProvider(
    //             bloc: homeDrawerBloc,
    //             child: HomeDrawer(),
    //           ),
    //           // child: Text("AAAAAAAAAAA",
    //           //     style: TextStyle(
    //           //       color: Colors.red,
    //           //       fontSize: 36,
    //           //     )),
    //         ),
    //       );
    //     });
  }

  initMainData() {}
}
